package com.tezign.intelligence.mini.dam.aifamilyconsultant.service.impl;

import com.tezign.intelligence.mini.dam.aifamilyconsultant.entity.Family;
import com.tezign.intelligence.mini.dam.aifamilyconsultant.mapper.FamilyMapper;
import com.tezign.intelligence.mini.dam.aifamilyconsultant.service.IFamilyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 家庭信息 服务实现类
 * </p>
 *
 * @author six
 * @since 2024-11-16
 */
@Service
@Slf4j
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, Family> implements IFamilyService {

    @Override
    public String generateNewCode() {
        // 根据时间戳生成家庭号 4位 0000-9999
        String code = String.format("%04d", System.currentTimeMillis() % 10000);
        final Family family = this.lambdaQuery().eq(Family::getCode, code).last("limit 1").one();
        if (family == null) {
            return code;
        }
        log.info("家庭号已存在，重新生成");
        return generateNewCode();
    }
}
